在看OSTEP的TLB部分时,作业中有一个问题是:如何让线程运行在特定的CPU上,觉得挺有趣的问题,随后通过STFW找到了答案。

主要用到两个 pthread_setaffinity_nppthread_getaffinity_np api。

通过 RTFM

1
man pthread_setaffinity_np

有一段示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#define _GNU_SOURCE
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#define handle_error_en(en, msg) \
do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)

int
main(int argc, char *argv[])
{
int s, j;
cpu_set_t cpuset;
pthread_t thread;

thread = pthread_self(); // 获取当前运行的线程数据结构

/* Set affinity mask to include CPUs 0 to 7 */
CPU_ZERO(&cpuset); // 将cpuset结构清零
// 将0-7号CPU全部加入cpuset中(如果只想指定特定的cpu,运行循环中那一行即可)
// 例如将线程运行在3号cpu上:CPU_SET(3, &cpuset);
for (j = 0; j < 8; j++)
CPU_SET(j, &cpuset); // 设置j号cpu到cpuset中。

// set CPU affinity of a thread
s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
if (s != 0)
handle_error_en(s, "pthread_setaffinity_np");

/* Check the actual affinity mask assigned to the thread */
s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
if (s != 0)
handle_error_en(s, "pthread_getaffinity_np");

printf("Set returned by pthread_getaffinity_np() contained:\n");
for (j = 0; j < CPU_SETSIZE; j++)
if (CPU_ISSET(j, &cpuset))
printf(" CPU %d\n", j);

exit(EXIT_SUCCESS);
}

如果gcc无法通过编译:undefined reference to pthread_setaffinity_np

在编译时加 -lpthread 参数即可。

stackoverflow: pinning-a-thread-to-a-core-in-a-cpuset-through-c